from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import QApplication, QMainWindow, qApp, QWidget,QTableWidgetItem
import sys
import pymysql
from PyQt5.QtGui import QKeyEvent
from PyQt5.QtCore import *

class EmittingStr(QtCore.QObject):
    textWritten = QtCore.pyqtSignal(str) #定义一个发送str的信号
    def write(self, text):
      self.textWritten.emit(str(text))

class Ui_MainWindow(QWidget):
    def __init__(self):
        super().__init__()
        self.setupUi(MainWindow)
        self.th = EmittingStr()
        self.th.textWritten.connect(self.outputWritten)              
        sys.stdout = self.th  
        
    def write(self, text):                                              #发送文本
        self.signalForText.emit(str(text))

    def outputWritten(self, text):                                      #输出到textBrowser
        cursor = self.textBrowser.textCursor()                          #获取指针对象
        # cursor.movePosition(QtGui.QTextCursor.End)                    #鼠标光标位置
        cursor.insertText(text)                                         #将外部数据放入指针内
        self.textBrowser.setTextCursor(cursor)                          #指针对象指向testbrowser
        # self.textBrowser.ensureCursorVisible()

    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(1070, 593)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.textBrowser = QtWidgets.QTextBrowser(self.centralwidget)
        self.textBrowser.setGeometry(QtCore.QRect(10, 0, 381, 481))
        font = QtGui.QFont()
        font.setFamily("微软雅黑")
        font.setPointSize(12)
        self.textBrowser.setFont(font)
        self.textBrowser.setObjectName("textBrowser")
        self.lineEdit = QtWidgets.QLineEdit(self.centralwidget)
        self.lineEdit.setGeometry(QtCore.QRect(300, 500, 541, 31))
        font = QtGui.QFont()
        font.setFamily("微软雅黑")
        font.setPointSize(12)
        self.lineEdit.setFont(font)
        self.lineEdit.setObjectName("lineEdit")
        self.pushButton = QtWidgets.QPushButton(self.centralwidget)
        self.pushButton.setGeometry(QtCore.QRect(840, 500, 75, 31))
        self.pushButton.setObjectName("pushButton")
        self.tableWidget = QtWidgets.QTableWidget(self.centralwidget)
        self.tableWidget.setGeometry(QtCore.QRect(390, 0, 671, 481))
        self.tableWidget.setObjectName("tableWidget")
        self.tableWidget.setColumnCount(0)
        self.tableWidget.setRowCount(0)
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(240, 500, 61, 31))
        font = QtGui.QFont()
        font.setFamily("微软雅黑")
        font.setPointSize(11)
        self.label.setFont(font)
        self.label.setObjectName("label")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 1070, 23))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.pushButton.setText(_translate("MainWindow", "屏幕清除"))
        self.label.setText(_translate("MainWindow", "sql命令："))


        self.lineEdit.returnPressed.connect(self.lineEdit_to_textBrowser)
        self.pushButton.clicked.connect(self.clear_screen)
        
    def clear_screen(self):
        self.textBrowser.clear()


    def lineEdit_to_textBrowser(self):
      try: 
        conn=pymysql.connect(
        host='localhost',
        port=3306,
        user='root',
        passwd='111',
        database = 'yyq',   #选择被操作的数据库
        charset='utf8',
        autocommit= True
        )
    
        cur=conn.cursor()
        cur.execute(str(ui.lineEdit.text()))
        result = cur.fetchall()
        row=cur.rowcount
        vol=len(result[0]) 

        list1 = []
        self.tableWidget.setRowCount(row)
        self.tableWidget.setColumnCount(vol)
        
        for m in range(row):
            for n in range(vol):
                data = result[m][n]
                data_change_to_str = QTableWidgetItem(str(data))
                self.tableWidget.addItem(m,n,data_change_to_str)
        # self.tableWidget.clear()
        # rows=len(result[0])  #新数据行数
        # self.tableWidget.setRowCount(rows)
        
        for i in result:
            print(i)
      except Exception as e:
            print(str(e))
      self.lineEdit.setText('')

if __name__ == "__main__":
    app = QApplication(sys.argv)
    MainWindow = QMainWindow()
    MainWindow.show()
    ui = Ui_MainWindow()
    sys.exit(app.exec_())
